package com.isunland.managesystem.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.CustomerNeed;
import com.isunland.managesystem.utils.MyStringUtil;
import java.util.List;

public class CustomerNeedAdapter extends ArrayAdapter<CustomerNeed> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-07-01 10:50:23 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    final TextView tvProductCustomerContact;
    final TextView tvUnitNameCustomerContact;
    final TextView tvUnitStatusCustomerContact;
    final TextView tvInfoSourceCustomerNeedAdapter;
    public final TextView ivLogo;

    private ViewHolder(LinearLayout view, TextView tvProductCustomerContact,
        TextView tvUnitNameCustomerContact, TextView tvCommunicationContentCustomerContact,
        TextView tvInfoSourceCustomerNeedAdapter, TextView ivLogo) {
      this.rootView = view;
      this.tvProductCustomerContact = tvProductCustomerContact;
      this.tvUnitNameCustomerContact = tvUnitNameCustomerContact;
      this.tvUnitStatusCustomerContact = tvCommunicationContentCustomerContact;
      this.tvInfoSourceCustomerNeedAdapter = tvInfoSourceCustomerNeedAdapter;
      this.ivLogo = ivLogo;
    }

    public static ViewHolder create(LinearLayout view) {
      TextView tvProductCustomerNeedAdapter =
          (TextView) view.findViewById(R.id.tv_product_customerNeedAdapter);
      TextView tvUnitNameCustomerNeedAdapter =
          (TextView) view.findViewById(R.id.tv_unitName_customerNeedAdapter);
      TextView tvUnitStatusCustomerNeedAdapter =
          (TextView) view.findViewById(R.id.tv_unitStatus_customerNeedAdapter);
      TextView tvInfoSourceCustomerNeedAdapter =
          (TextView) view.findViewById(R.id.tv_infoSource_customerNeedAdapter);
      TextView ivLogo = (TextView) view.findViewById(R.id.iv_logo);
      return new ViewHolder(view, tvProductCustomerNeedAdapter, tvUnitNameCustomerNeedAdapter,
          tvUnitStatusCustomerNeedAdapter, tvInfoSourceCustomerNeedAdapter, ivLogo);
    }
  }

  @NonNull @Override
  public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_customer_need, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    CustomerNeed item = getItem(position);
    if (item == null) return vh.rootView;
    vh.ivLogo.setText(MyStringUtil.substring(item.getNeedProductDesc(), 0, 1));
    vh.tvProductCustomerContact.setText(item.getNeedProductDesc());
    vh.tvUnitNameCustomerContact.setText(item.getCustomerName());
    vh.tvUnitStatusCustomerContact.setText(item.getCustomerStatusText());
    vh.tvInfoSourceCustomerNeedAdapter.setText(item.getCustomerInfoFromText());

    return vh.rootView;
  }

  private LayoutInflater mInflater;

  // Constructors
  public CustomerNeedAdapter(Context context, List<CustomerNeed> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public CustomerNeedAdapter(Context context, CustomerNeed[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}
